
import { _decorator, Component, Node, NodePool, instantiate, Vec3, random, randomRange, resources, Prefab, randomRangeInt } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = test
 * DateTime = Thu Dec 23 2021 18:40:20 GMT+0800 (中国标准时间)
 * Author = linruimin
 * FileBasename = test.ts
 * FileBasenameNoExtension = test
 * URL = db://assets/test.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('test')
export class test extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;

    arrName = ['kulou', 'cocosWalk', 'shitouWalk', 'altman', 'soldier', 'clown']

    arrModel: Node[] = [];

    nodePool = new NodePool();

    @property(Node)
    modelParent = null;

    start () {
        this.arrName.forEach((name)=>{
            resources.load(`model/${name}`, Prefab, (err, prefab) => {
                const newNode = instantiate(prefab);
                this.arrModel.push(newNode);
            });
        });
    }

    onClickPut()
    {
        for (let i = 0; i < 100; i++)
        {
            this.randomPutNode();
        }
    }

    onClickPut1000()
    {
        for (let i = 0; i < 1000; i++)
        {
            this.randomPutNode();
        }
    }

    randomPutNode()
    {
        let cloneNode = instantiate(this.arrModel[randomRangeInt(0, this.arrModel.length)]);
        this.nodePool.put(cloneNode);
    }

    onClickGet()
    {
        console.log(this.nodePool.size());
        while(this.nodePool.size() > 0)
        {
            console.log(this.nodePool.size());
            let node = this.nodePool.get();
            node.parent = this.modelParent;
            node.position = new Vec3(randomRange(-4.0, 4.0), randomRange(-4.0, 4.0), randomRange(-4.0, 4.0));
        }
    }

    onClickClear()
    {

    }

    // update (deltaTime: number) {
    //     // [4]
    // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
